const { get_fst_file } = require("../utils/file_tool");
const { BaseCommand } = require("./base");
const path = require("node:path");
const fs = require("node:fs");
const sizeOf = require("image-size");

class FileInfoCommand extends BaseCommand {
    async execute() {
        const file = get_fst_file(this.selfData.src);
        if (!file) {
            return null;
        }
        let dimensions;
        try {
            dimensions = sizeOf(file);
        } catch (_err) {}
        const stat = fs.statSync(file);
        const ret = {
            fullpath: file,
            extname: path.extname(file),
            basename: path.basename(file),
            basename_noext: path.basename(file, path.extname(file)),
            dirname: path.dirname(file),
            isDirectory: stat.isDirectory(),
            isFile: stat.isFile(),
            atime: stat.atime,
            mtime: stat.mtime,
            ctime: stat.ctime,
            size: stat.size,
            size_kb: +(stat.size / 1024).toFixed(2),
            dimensions: dimensions,
        };
        return ret;
    }
}

module.exports = {
    FileInfoCommand,
};
